<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import {
  flowLineAverage,
  flowLineMax,
  flowLineMin,
} from "@/api/data_analysis/baseData";

import * as echarts from "echarts";

require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "380px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    baseDataParam: {
      type: Object,
      default: {},
    },
    // 聚合类型
    polymersType: {
      type: "average" | "max" | "min",
      default: "average",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      console.log("子组件调用了方法");
      this.chart = echarts.init(this.$el, "macarons");
      // 平均值
      if (this.polymersType === "average") {
        flowLineAverage(this.baseDataParam).then((res) => {
          console.info(res);
          this.chart.showLoading({ text: "正在努力的读取数据中..." });
          this.chart.setOption(res);
          this.chart.hideLoading();
        });
      }
      // 最大值
      if (this.polymersType === "max") {
        flowLineMax(this.baseDataParam).then((res) => {
          console.info(res);
          this.chart.showLoading({ text: "正在努力的读取数据中..." });
          this.chart.setOption(res);
          this.chart.hideLoading();
        });
      }
      // 最小值
      if (this.polymersType === "min") {
        flowLineMin(this.baseDataParam).then((res) => {
          console.info(res);
          this.chart.showLoading({ text: "正在努力的读取数据中..." });
          this.chart.setOption(res);
          this.chart.hideLoading();
        });
      }
    },
  },
};
</script>
